{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 温度转换\n",
    "\n",
    "编写摄氏温度和华氏温度的转换程序，要求如下\n",
    "\n",
    "* 输入摄氏温度的格式为：26C，输入后转换成为华氏温度\n",
    "* 输入华氏温度的格式为：70F，输入后转换成为摄氏温度\n",
    "* 摄氏温度到华氏温度的转换公式为：\n",
    "\n",
    "```script\n",
    "f = c * 1.8 + 32\n",
    "```\n",
    "\n",
    "* 华氏温度到摄氏温度的转换公式为：\n",
    "\n",
    "```script\n",
    "c = (f - 32) / 1.8\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = input(\"请输入要转换的温度（摄氏：32C；华氏：70F）\")\n",
    "\n",
    "if tmp[-1] in (\"C\", \"c\"):\n",
    "    f = int(tmp[:-1]) * 1.8 + 32\n",
    "    print(f\"转换的温度为：{f}C\")\n",
    "elif tmp[-1] in (\"F\", \"f\"):\n",
    "    c = (int(tmp[:-1]) - 32) / 1.8\n",
    "    print(f\"转换的温度为：{c}C\")\n",
    "else:\n",
    "    print(\"输入错误\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "重构温度转换程序，把相关操作嵌入到循环中，当输入\"quit\"中断循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    tmp = input(\"请输入要转换的温度（摄氏：32C；华氏：70F）\")\n",
    "\n",
    "    if tmp.strip().lower() == \"quit\":\n",
    "        break\n",
    "\n",
    "    if tmp[-1] in (\"C\", \"c\"):\n",
    "        f = int(tmp[:-1]) * 1.8 + 32\n",
    "        print(f\"转换的温度为：{f}C\")\n",
    "    elif tmp[-1] in (\"F\", \"f\"):\n",
    "        c = (int(tmp[:-1]) - 32) / 1.8\n",
    "        print(f\"转换的温度为：{c}C\")\n",
    "    else:\n",
    "        print(\"输入错误\")\n",
    "\n",
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 汇率转换\n",
    "\n",
    "编写人民币和美元之间的互转，按 $1 = ￥6.5 进行换算；要求实现循环转换，输入“quit”退出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重构，函数编程\n",
    "\n",
    "分别把汇率转换程序和温度转换程序进行函数式重构！要求：\n",
    "\n",
    "* 把输入功能重构为函数\n",
    "* 把摄氏温度转换和华氏温度转换重构为函数\n",
    "* 把美元转换和人民币转换重构为函数\n",
    "* 把各转换功能嵌入循环输入“quit”退出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 面向对象重构"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d80dceb8e76061e928794bb3ce8fa9d0c2b7abe9c550ab61d3fca5a520d3bcb0"
  },
  "kernelspec": {
   "display_name": "Python 3.8.6 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
